package com.shuiyun.chat.handler;

import com.shuiyun.chat.entity.vo.UserVO;
import com.shuiyun.chat.service.UserService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: chatServer
 * @description: 退出处理器
 * @author: shuiyun
 * @create: 2021-05-12 22:21
 **/
@Slf4j
@ChannelHandler.Sharable
@Component
public class QuitHandler extends ChannelInboundHandlerAdapter {

    @Autowired
    private UserService userService;

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        UserVO user = userService.getUserByChannel(ctx.channel());
        if (null != user){
            log.info("{} 下线了！！",user.getNickName());
        }else{
            log.error("查询当前用户失败");
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        UserVO user = userService.getUserByChannel(ctx.channel());
        if (null != user){
            log.info("{} 掉线了！！",user.getNickName());
        }
    }
}
